<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property int $user_id 
 * @property string $wx_openid 
 * @property string $wx_nickname 
 * @property string $wx_avatar 
 * @property int $wx_issub 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property string $deleted_at 
 */
class UserWechat extends Model
{
    use SoftDeletes;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user_wechat';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'wx_openid',
        'wx_nickname',
        'wx_avatar',
        'user_id'
    ];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'int', 'user_id' => 'integer', 'wx_issub' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    public function getWxAvatarAttribute($field){
        if($field){
            preg_match("/(?:[http|https]:\/\/)([^\/]+)/", $field, $matched);
            if(empty($matched)){
                return $this->attributes[$field] = config('common.image_prefix').$field;
            }
            return $this->attributes[$field]=$field;
        }
        return $this->attributes[$field] = null;
    }
}